//
//  AdviceBackViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "AdviceBackViewController.h"
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
#import "FitHeader.h"
#import "BaseLabel.h"
#import "Model.h"

@interface AdviceBackViewController ()<MFMailComposeViewControllerDelegate>

@property(nonatomic, strong)UITextView *messageTextView;
@property(nonatomic, strong)BaseLabel *label;

@end

@implementation AdviceBackViewController

-(void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [Model shareInstance].ViewColor;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.84 green:0.23 blue:0 alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    [self.navigationItem.rightBarButtonItem setTintColor:[Model shareInstance].TitleColor];
    _label.textColor = [Model shareInstance].TitleColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction:)];
    self.navigationItem.title = @"意见反馈";
}
- (void)loadView{
    [super loadView];
    
    _label = [[BaseLabel alloc]initWithFrame:CGRectMake(20 * FITWidth, 10 * FITHeight, 355 * FITWidth, 40 * FITHeight)];
    _label.text = @"请输入您要反馈的信息:";
    [self.view addSubview:_label];
    
    _messageTextView = [[UITextView alloc]initWithFrame:CGRectMake(20 * FITWidth, 60 * FITHeight, self.view.frame.size.width - 40 * FITWidth, 150 * FITHeight)];
    _messageTextView.backgroundColor = [UIColor whiteColor];
    _messageTextView.font = [UIFont fontWithName:@"HelveticaNeue" size:14 * FITWidth];
    [_messageTextView becomeFirstResponder];
    [self.view addSubview:_messageTextView];
}
#pragma mark - 发送反馈信息
- (void)sendAction:(id)sender{
    if (![self.messageTextView.text isEqualToString:@""]) {
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass != nil) {
            if ([mailClass canSendMail]) {
                [self displayComposeerSheet];
            }else{
                [self launchMaiAppOnDevice];
            }
        }else{
            [self launchMaiAppOnDevice];
        }
    }else{
        
//        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"反馈信息不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alter addAction:cancelAction];
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"反馈信息不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:NO];
    }
}
-(void)displayComposeerSheet{
    MFMailComposeViewController *piker = [[MFMailComposeViewController alloc]init];
    piker.mailComposeDelegate = self;
    [piker setSubject:@"邮件主题"];
    NSArray *toRecipPient = [NSArray arrayWithObject:@"zc877386629@163.com"];
    [piker setToRecipients:toRecipPient];
    NSString *emailBody = self.messageTextView.text;
    [piker setMessageBody:emailBody isHTML:YES];
    [self presentViewController:piker animated:YES completion:nil];
    
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"邮件发送失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        
        
        //        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"邮件发送失败" preferredStyle:UIAlertControllerStyleActionSheet];
        //        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        //        [alertVC addAction:cancelAction];
        //        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
    _messageTextView.hidden = NO;
    switch (result) {
        case MFMailComposeResultSent:
            NSLog(@"发送成功");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"发送失败");
            break;
        case MFMailComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"保存成功");
            break;
            
        default:
            break;
    }
    if (error) {
        NSLog(@"错误信息:%@", error.localizedDescription);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)launchMaiAppOnDevice{
    NSString *recipients = @"mailto:zc877386629@163.com&subject=用户反馈!";
    NSString *body = [NSString stringWithFormat:@"&body=%@", _messageTextView.text];
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:email]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
